package com.pets.service.Impl;

import com.pets.dao.ApplyMapper;
import com.pets.dao.UsersMapper;
import com.pets.entity.Apply;
import com.pets.entity.Users;
import com.pets.enums.YesNoEnum;
import com.pets.qo.AuditApplyQO;
import com.pets.service.AdminUsersService;
import com.pets.util.Result;
import com.pets.vo.UserListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * 后台管理系统操作用户service
 */
@Service
public class AdminUsersServiceImpl implements AdminUsersService {

    @Resource
    private UsersMapper usersMapper;

    @Resource
    private ApplyMapper applyMapper;

    @Override
    public List<UserListVO> getUserlist() {
        List<UserListVO> userListVOS = new ArrayList<>();
        //查询所有用户
        List<Users> usersList = usersMapper.getAllUserlist();
        //查询所有申请中的申请表记录信息
        List<Apply> applies = applyMapper.getAllApply();
        Map<Integer, Apply> applyMap = new HashMap<>();
        for (int i = 0; i < applies.size(); i++) {
            applyMap.put(applies.get(i).getUid(), applies.get(i));
        }
        for (int i = 0; i < usersList.size(); i++) {
            UserListVO userListVO = new UserListVO();
            //拷贝相同属性 效果等同于userListVO.setUid(usersList.get(i).getUid());
            BeanUtils.copyProperties(usersList.get(i),userListVO);
            //containsKey 判断map是否存在这个key
            if(applyMap.containsKey(usersList.get(i).getUid())){
                userListVO.setApplyFlag(true);
            }
            userListVOS.add(userListVO);
        }
        return userListVOS;
    }

    @Override
    public Result editUserStatus(int uId, int status) {
        Users users = usersMapper.selectByPrimaryKey(uId);
        users.setUstatus(status);
        int i = usersMapper.updateByPrimaryKey(users);
        Result result = new Result();
        if(i > 0){
            result.setCode(YesNoEnum.YES.getCode());
        }else {
            result.setCode(YesNoEnum.NO.getCode());
        }
        return result;
    }

    @Transactional
    @Override
    public Result auditApply(AuditApplyQO auditApplyQO) {
        //获取审核记录
        Apply apply = applyMapper.selectByPrimaryKey(auditApplyQO.getAppId());
        apply.setAppstatus(auditApplyQO.getAppStatus());
        apply.setAppaduitopinion(auditApplyQO.getAppAduitOpinion());
        apply.setAppaudituser(auditApplyQO.getAppAuditUser());
        apply.setAppaudittime(new Date());
        if(auditApplyQO.getAppStatus().equals("1")){
            Users  users = usersMapper.selectByPrimaryKey(apply.getUid());
            users.setUtype(0);
            usersMapper.updateByPrimaryKey(users);
        }

        Result result = new Result();
        int i = applyMapper.updateByPrimaryKey(apply);
        if(i > 0){
            result.setCode(YesNoEnum.YES.getCode());
            result.setMsg("操作成功");
        }else {
            result.setCode(YesNoEnum.NO.getCode());
            result.setMsg("操作失败");
        }
        return result;
    }
}
